package com.glory.erp.auth.menu.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.glory.erp.auth.menu.dao.MenuDao;
import com.glory.erp.auth.menu.bean.Menu;
import com.glory.erp.auth.menu.bean.MenuQuery;
import com.glory.erp.auth.base.BaseDaoImpl;
import com.glory.erp.auth.base.BaseQueryModel;
import com.glory.erp.auth.emp.bean.Emp;

public class MenuDaoImpl extends BaseDaoImpl<Menu> implements MenuDao {

	public void doQbc(DetachedCriteria dc, BaseQueryModel qm) {
		MenuQuery mqm = (MenuQuery) qm;
		// uuid 不等于 1
		dc.add(Restrictions.not(Restrictions.eq("uuid",
				Menu.MENU_SYSTEM_MENU__UUID)));
		if (mqm.getName() != null && mqm.getName().trim().length() > 0) {
			dc.add(Restrictions.like("name", "%" + mqm.getName().trim() + "%"));
		}
		if (mqm.getParent() != null && mqm.getParent().getUuid() != null
				&& mqm.getParent().getUuid() != -1) {
			dc.add(Restrictions.eq("parent", mqm.getParent()));
		}
	}

	public List<Menu> findByPuuidIsOneOrZero() {
		String hql = "from Menu where parent.uuid = ? or uuid = ?";
		return this.getHibernateTemplate().find(hql,
				Menu.MENU_SYSTEM_MENU__UUID, Menu.MENU_SYSTEM_MENU__UUID);
	}

	public List<Menu> findAllOneLevelByEmpUuid(Long uuid) {
		String hql = "select distinct menu from Menu menu join menu.roles role join role.emps emp where emp.uuid = ? and menu.parent.uuid = ? order by menu.uuid";
		return this.getHibernateTemplate().find(hql,uuid,Menu.MENU_SYSTEM_MENU__UUID);
	}

	public List<Menu> findByEmpUuidAndPuuid(Long uuid, Long puuid) {
		String hql ="select distinct menu from Menu menu join menu.roles role join role.emps emp where emp.uuid = ? and menu.parent.uuid = ? order by menu.uuid";
		return this.getHibernateTemplate().find(hql,uuid,puuid);
	}
}